# _*_ coding: utf-8 _*_
# @Time : 2024-01-16 21:12
# @Author : wf

import urllib.request as request
import urllib.parse

headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36'
}


def create_request(page):
    base_url = 'https://movie.douban.com/j/chart/top_list?type=5&interval_id=100%3A90&action=&'
    data = {
        'start': (page - 1) * 20,
        'limit': '20'
    }
    base_url += urllib.parse.urlencode(data)
    print(base_url)

    return request.Request(url=base_url, headers=headers)


def get_content(req: request.Request):
    response = request.urlopen(req)

    return response.read().decode('utf-8')


def download(content, page):
    with open('douban_' + str(page) + '.json', 'w', encoding='utf-8') as fp:
        fp.write(content)


if __name__ == '__main__':
    start_page = int(input("起始页数："))
    end_page = int(input("结束页数："))
    for page in range(start_page, end_page + 1):
        req = create_request(page)

        content = get_content(req)

        # 下载文件
        download(content, page)
